package com.kusiri.kframework.component.library.table
{
	import com.kusiri.kframework.event.KfValueObjectEvent;
	import com.kusiri.kframework.model.IKfValueObject;
	import com.kusiri.kframework.model.IKfValueObjectList;
	import com.kusiri.kframework.ui.KfUiComponent;
	
	import mx.core.UIComponent;

	public class TableComponent extends KfUiComponent 
	{
		private var self : TableComponent;
		
		
		/**
		 * This is the configuration
		 * These properties will be available to inspect in the UI builder
		 */ 
	
		public var valueObjectList : IKfValueObjectList;
		
		/**
		 * This is the UIComponent, set in the constructor
		 */
		private var _uiComponent : TableComponentUi; 
	
		/**
		 * Standard UI Component stuff
		 */		

		/**
		 * Set up the ui component
		 */
		public function TableComponent() : void
		{
			self = this;
			
			_uiComponent = new TableComponentUi();
			_uiComponent.controller = this;
		}

		/**
		 * Get the Flex UI Component
		 */
		override public function get __uiComponent() : UIComponent
		{
			return _uiComponent;
		}

		/**
		 * Tell the UI Component to load its data.
		 */
		override public function __initializeComponent(onSuccess : Function, onFail : Function) : void
		{
			
			if (!valueObjectList)
			{
				onSuccess(this);
				return;	
			} 
			
			
			
			valueObjectList.__initializeComponent(
				function(valueObjectSet:IKfValueObjectList) : void
				{
					_uiComponent.dataProvider = valueObjectSet.valueObjects;
					onSuccess(self);
				},
				function(error:*) : void
				{
					onFail(self, error);
				}
			);
		}
		
		public function onRowDoubleClick(valueObject : IKfValueObject) : void
		{
			var event : KfValueObjectEvent = new KfValueObjectEvent();
			event.valueObject = valueObject;
			event.__name = 'rowDoubleClick';			
			__eventActions.raiseEvent(event);
			
		}
		
	}
}